package com.codechuan.demo.ui.home;

import static android.os.Looper.getMainLooper;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codechuan.demo.R;
import com.codechuan.demo.adapter.ScheduleSelectBaseAdapter;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.databinding.FragmentHomeBinding;
import com.codechuan.demo.dto.CityInfo;
import com.codechuan.demo.dto.SysSchedule;
import com.codechuan.demo.dto.SysUserConfig;
import com.codechuan.demo.dto.TokenObject;
import com.codechuan.demo.floatWindowManager.FloatWindowManager;
import com.codechuan.demo.floatWindowManager.log.LogManager;
import com.codechuan.demo.thread.MainThread;
import com.codechuan.demo.ui.login.LoginActivity;
import com.codechuan.demo.utils.DataCheckUtil;
import com.codechuan.demo.utils.DateUtil;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.HttpControllerUtil;
import com.codechuan.demo.utils.MyHttpUtil;
import com.example.libbaiduselectlocation.SelectLocationBDActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class HomeFragment extends Fragment implements View.OnClickListener {

    private FragmentHomeBinding binding;
    private Button startCore;
    public static TextView address;
    private Button ssdwButton;
    String jsonObjectString;

    private SimpleDateFormat simpleDateFormat;
    private Handler mainHandler;

    public static void updateStartAddress(String substring) {
        address.setText(substring);
    }

    @SuppressLint("SimpleDateFormat")
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        if (simpleDateFormat == null)
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        if (Constant.button == null)
            Constant.button = startCore;

        startCore = binding.startCore;
        startCore.setOnClickListener(this);
        // 赋值常量按钮
        Constant.button = startCore;
        // 设置消息提示
        Constant.errorMsg = binding.errorMsg;
        if (Constant.isStart)
            startCore.setText("停止抢单");
        if (!Constant.isStart)
            startCore.setText("开始抢单");

        binding.position.setOnClickListener(this);
        binding.js.setOnClickListener(this);
        ssdwButton = binding.ssdwButton;
        ssdwButton.setOnClickListener(this);
        mainHandler = new Handler(getMainLooper()); //获取主线程
        address = binding.startAddressText;
        // 选择时间按钮
        Button selectStartTime = binding.selectStartTime;
        selectStartTime.setOnClickListener(this);
        Button selectEndTime = binding.selectEndTime;
        selectEndTime.setOnClickListener(this);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    // 在Activity中调用此方法
    private void openWebPage(String url) {
        if (url != null && !url.trim().isEmpty()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        // 开始抢单按钮
        if (R.id.startCore == id) {
            if (!Constant.is_zx) {
                Constant.handler.post(() -> {
                    DialogUtil.showDialog(Constant.context, "当前版本不是最新,请及时更新!");
                });
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                Constant.handler.post(() -> {
                    DialogUtil.showDialog(Constant.context, "即将为你打开下载网址!");
                });
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                Constant.handler.post(() -> {
                    openWebPage("http://45.251.9.55:9999");
                });
                return;
            }
            if (Constant.isStart) {
                FloatWindowManager.get().stopAnimator();
                LogManager.get().stopLog();
                DialogUtil.showDialog(Constant.context, "关闭算法中,请等待");
                Constant.isStart = false;
                try {
                    // 延迟三秒避免内存泄露
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                startCore.setText("开始抢单");
                Constant.handler.post(() -> {
                    DialogUtil.showDialog(Constant.context, "百里停止抢单...");
                });
            } else {
                LogManager.get().startLog();
                // 判断单价是否为浮点数
                if (binding.djCheckBox.isChecked()) {
                    Constant.DJ_CHECK = true;
                    Constant.DJ_MIN_MONEY = Double.parseDouble(binding.minDjMoney.getText().toString());
                }

                // 判断是不是实时定位
                if (binding.ssdw.isChecked()) {
                    Constant.SS_DW_CHECKED = true;
                } else {
                    Constant.SS_DW_CHECKED = false;
                }

                // 判断是否设置指定城市
                Constant.PC_CS_CHECK = binding.zdCsCheck.isChecked();
                if (binding.zdCsCheck.isChecked()) {
                    Constant.handler.post(() -> {
                        Constant.handler.post(() -> {
                            DialogUtil.showDialog(Constant.context, "开始加入指定城市...");
                        });
                    });
                    // 获取城市信息
                    String zdCsText = binding.zdCsText.getText().toString();
                    // 把#号替换成,
                    Constant.ZD_CS_TEXT = zdCsText.replaceAll("#", ",");
                    Constant.executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                            String s = zdCsText.replaceAll("#", ",");
                            if (s.contains(",")){
                                String[] split = s.split(",");
                                for (String cityCode : split) {
                                    CityInfo cityInfo = HttpControllerUtil.getCityInfo(cityCode);
                                    Constant.cityCodeList.add(cityInfo.getCityId());
                                }
                            }else {
                                CityInfo cityInfo = HttpControllerUtil.getCityInfo(s);
                                Constant.cityCodeList.add(cityInfo.getCityId());
                            }
                        }
                    });
                }

                // 判断是否设置指定城市
                Constant.PC_CS_CHECK = binding.pcCsCheck.isChecked();
                if (binding.pcCsCheck.isChecked()) {
                    Constant.handler.post(() -> {
                        Constant.handler.post(() -> {
                            DialogUtil.showDialog(Constant.context, "开始加入排除城市...");
                        });
                    });
                    // 获取城市信息
                    String pcCsText = binding.pcCsText.getText().toString();
                    Constant.PC_CS_TEXT = pcCsText.replaceAll("#", ",");
                }

                // 置空主号失败次数和判断主号是否存在
                Constant.MAIN_ERROR_NUMBER = 0;
                if (Constant.MAIN_TOKEN == null || Constant.MAIN_TOKEN.isEmpty()) {
                    Constant.handler.post(()->{
                        DialogUtil.showDialog(Constant.context, "主号登录过期,或未登录.");
                    });
                    return;
                }

                // 开始校验金钱规则
                TextView minMoney = binding.minMoney;
                if (!DataCheckUtil.minNumberLsLessThanZeroOrIsNull(Integer.valueOf(minMoney.getText().toString()))) {
                    Constant.handler.post(()->{
                        DialogUtil.showDialog(Constant.context, "最小金额不能为0也不能为空");
                    });
                    return;
                }

                TextView maxMoney = binding.maxMoney;
                if (!DataCheckUtil.minNumberLsLessThanZeroOrIsNull(Integer.valueOf(maxMoney.getText().toString()))) {

                    Constant.handler.post(()->{
                        DialogUtil.showDialog(Constant.context, "最大金额不能为0也不能为空");
                    });
                    return;
                }
                Constant.MIN_MONEY = Integer.valueOf(minMoney.getText().toString());
                Constant.MAX_MONEY = Integer.valueOf(maxMoney.getText().toString());


                // 开始时间/结束时间校验规则
                EditText startTime = binding.startTime;
                EditText endTime = binding.endTime;
                if (!DataCheckUtil.dateCheck(startTime.getText().toString()) || !DataCheckUtil.dateCheck(endTime.getText().toString())) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context,"开始时间或结束时间设置错误");
                    });
                    return;
                }
                Constant.START_TIME = DateUtil.passDate(startTime.getText().toString());
                Constant.END_TIME = DateUtil.passDate(endTime.getText().toString());


                // 最大起点距离/最大终点距离校验规则
                if (!DataCheckUtil.minNumberLsLessThanZeroOrIsNull(Integer.valueOf(binding.maxStartingPointDistance.getText().toString()))) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context, "起点距离不能为0也不能为空");
                    });
                    return;
                } else {
                    Constant.MAX_STARTING_POINT_DISTANCE = binding.maxStartingPointDistance.getText().toString();
                }

                // 排序规则
                Constant.ZN_PX_CHECK = binding.znpx.isChecked();
                Constant.ZZ_CHECK = binding.zz.isChecked();
                Constant.ZJ_CHECK = binding.zj.isChecked();
                Constant.ZG_CHECK = binding.zg.isChecked();
                if (!Constant.ZN_PX_CHECK && !Constant.ZZ_CHECK && !Constant.ZJ_CHECK && !Constant.ZG_CHECK) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context,"订单排序规则必须选择一个.");
                    });
                    return;
                }
                if (Constant.ZN_PX_CHECK)
                    Constant.SORT = 1;
                if (Constant.ZZ_CHECK)
                    Constant.SORT = 2;
                if (Constant.ZJ_CHECK)
                    Constant.SORT = 3;
                if (Constant.ZG_CHECK)
                    Constant.SORT = 4;

                // 独享/拼车/舒适 人数规则选择框
                boolean pcPersonCheckBox = binding.pcPersonCheckBox.isChecked();
                boolean dxPersonCheckBox = binding.dxPersonCheckBox.isChecked();
                boolean ssPersonCheckBox = binding.ssPersonCheckBox.isChecked();
                if (!pcPersonCheckBox && !dxPersonCheckBox && !ssPersonCheckBox) {
                    Constant.handler.post(()->{
                        DialogUtil.showDialog(Constant.context, "独享,拼车,舒适模型必须选一个.");
                    });
                    return;
                }
                // 拼车人数校验
                Constant.PC_PERSON_CHECKBOX = false;
                if (pcPersonCheckBox) {
                    Constant.PC_PERSON_CHECKBOX = true;
                    Integer minPcNumber = Integer.valueOf(binding.minPcNumber.getText().toString());
                    Integer maxPcNumber = Integer.valueOf(binding.maxPcNumber.getText().toString());
                    if (!DataCheckUtil.minNumberLsLessThanZeroOrIsNull(minPcNumber) || !DataCheckUtil.maxNumberLsLessThanZeroOrIsNull(maxPcNumber)) {
                        Constant.handler.post(() -> {
                            DialogUtil.showDialog(Constant.context,"拼车人数设置错误.");
                        });
                        return;
                    }
                    Constant.PC_MIN_NUMBER = minPcNumber;
                    Constant.PC_MAX_NUMBER = maxPcNumber;
                }

                // 独享人数校验
                Constant.DX_PERSON_CHECKBOX = false;
                if (dxPersonCheckBox) {
                    Constant.DX_PERSON_CHECKBOX = true;
                    Integer minDxNumber = Integer.valueOf(binding.minDxNumber.getText().toString());
                    Integer maxDxNumber = Integer.valueOf(binding.maxDxNumber.getText().toString());
                    if (!DataCheckUtil.minNumberLsLessThanZeroOrIsNull(minDxNumber) || !DataCheckUtil.maxNumberLsLessThanZeroOrIsNull(maxDxNumber)) {
                        Constant.handler.post(() -> {
                            DialogUtil.showDialog(Constant.context, "独享人数设置错误.");
                        });
                        return;
                    }
                    Constant.DX_MIN_NUMBER = minDxNumber;
                    Constant.DX_MAX_NUMBER = maxDxNumber;
                }

                // 舒适人数校验
                Constant.SS_PERSON_CHECKBOX = false;
                if (ssPersonCheckBox) {
                    Constant.SS_PERSON_CHECKBOX = true;
                    Integer minSsNumber = Integer.valueOf(binding.minSsNumber.getText().toString());
                    Integer maxSsNumber = Integer.valueOf(binding.maxSsNumber.getText().toString());
                    if (!DataCheckUtil.minNumberLsLessThanZeroOrIsNull(minSsNumber) || !DataCheckUtil.maxNumberLsLessThanZeroOrIsNull(maxSsNumber)) {
                        Constant.handler.post(() -> {
                            DialogUtil.showDialog(Constant.context, "舒适人数设置错误.");
                        });
                        return;
                    }
                    Constant.SS_MIN_NUMBER = minSsNumber;
                    Constant.SS_MAX_NUMBER = maxSsNumber;

                }
                if (!binding.appController.isChecked() && !binding.xcxController.isChecked()) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context,"请选择App或小程序刷脸方式.");
                    });
                    return;
                }
                if (!binding.cj.isChecked() && !binding.sn.isChecked() && !binding.xc.isChecked()) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context,"请选择市内接单或者城际接单方式.");
                    });
                    return;
                }
                // 设置市内或者城际订单信息
                if (binding.cj.isChecked()) {
                    Constant.requestType = "3";
                    Constant.requestTypeParam = "requestType3Param";
                }
                if (binding.sn.isChecked()) {
                    Constant.requestType = "2";
                    Constant.requestTypeParam = "requestType2Param";
                }

                // 城市信息非空校验规则
                if (Constant.cityInfo == null) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context,"请先定位城市.");
                    });
                    return;
                }

                // 设置回显内容和全局控制
                Constant.CJ_ORDER = binding.cj.isChecked();
                Constant.SN_ORDER = binding.sn.isChecked();
                Constant.XC_ORDER = binding.xc.isChecked();
                Constant.ZJ_DX = binding.zjdxd.isChecked();
                Constant.YD_GS = binding.ydgsf.isChecked();
                Constant.LX_QD = binding.lxqdd.isChecked();
                Constant.APP_IS_CHECK = binding.appController.isChecked();
                Constant.XCX_IS_CHECK = binding.xcxController.isChecked();
                Constant.START_ADDRESS = binding.startAddressText.getText().toString();
                Constant.START_TIME_STRING = binding.startTime.getText().toString();
                Constant.END_TIME_STRING = binding.endTime.getText().toString();
                Constant.SX_NUMBER = binding.jsNumber.getText().toString();
                Constant.DJ_CHECK = binding.djCheckBox.isChecked();
                Constant.ZD_CS_CHECK = binding.zdCsCheck.isChecked();
                Constant.ZD_FB_XC = binding.zdFbXc.isChecked();
                // 判断是否是小程序的城际或者市内刷单类型
//                if (Constant.XCX_IS_CHECK) {
//                    if (Constant.SN_ORDER || Constant.CJ_ORDER) {
//                        if (Constant.SCHEDULE_ID == null || Constant.SCHEDULE_ID.isEmpty()) {
//                            DialogUtil.showDialog(Constant.context,"小程序需要去行程管理选择一个行程再刷单.");
//                            return;
//                        }
//                    }
//                }
                // 判断刷新延时是否大于1000
                if (Integer.parseInt(Constant.SX_NUMBER) >= 1000) {
                    DialogUtil.showDialog(Constant.context,"刷新延时不能高于1000,单账号安全值为500.");
                    return;
                }
                // 判断是否是行程
                if (Constant.XC_ORDER) {
                    View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.activity_select_schedule, null);
                    // 判断城市是否定位
                    if (Constant.cityInfo == null) {
                        DialogUtil.showDialog(Constant.context, "请先定位城市.");
                        return;
                    }
                    // 查询行程数据
                   Constant.executorService.execute(new Runnable() {
                       @Override
                       public void run() {
                           jsonObjectString = HttpControllerUtil.getWeChatMiniMyOrderOrMyScheduleOrder();


                           if (jsonObjectString == null) {
                               DialogUtil.showDialog(Constant.context, "系统异常.");
                           }
                           JSONObject jsonObject = null;
                           try {
                               jsonObject = JSONObject.parseObject(jsonObjectString);
                           } catch (Exception e) {
                               Constant.handler.post(() -> DialogUtil.showDialog(Constant.context, "哈喽官方提示:" + jsonObjectString));
                               return;
                           }
                           JSONArray data = null;
                           if (jsonObject == null) {
                               Constant.handler.post(() -> DialogUtil.showDialog(getActivity(), "获取失败,请再次尝试."));
                               return;
                           }
                           if (jsonObject.getString("code").equals("0")) {
                               data = jsonObject.getJSONArray("data");
                               if (data.isEmpty()) {
                                   Constant.handler.post(()->{
                                       DialogUtil.showDialog(Constant.context, "暂无行程,请前往行程管理页面发布行程");
                                   });
                                   Intent intent = new Intent();
                                   intent.setClass(Constant.context, LoginActivity.class);
                                   startActivity(intent);
                               }
                           }
                           // 置空行程id避免重复加入
                           Constant.xc_id = new ArrayList<>();

                           ArrayList<SysSchedule> scheduleArrayList = new ArrayList<>();
                           assert data != null;
                           for (Object scheduleDataList : data) {
                               JSONObject schedule = JSONObject.parseObject(scheduleDataList.toString());
                               JSONObject startPosition = schedule.getJSONObject("startPosition");
                               JSONObject endPosition = schedule.getJSONObject("endPosition");

                               SysSchedule sysSchedule = new SysSchedule();
                               sysSchedule.setDriverJourneyGuid(schedule.getString("driverJourneyGuid"));
                               sysSchedule.setStartCityName(startPosition.getString("cityName") + startPosition.getString("adName"));
                               sysSchedule.setEndCityName(endPosition.getString("cityName") + endPosition.getString("adName"));
                               sysSchedule.setSonOrderNumber(schedule.getJSONArray("paxOrderList").size());
                               scheduleArrayList.add(sysSchedule);
                           }
                          Constant.handler.post(()->{
                              // 创建自定义适配器对象
                              ListView listView = inflate.findViewById(R.id.selectScheduleList);
                              ScheduleSelectBaseAdapter scheduleBaseAdapter = new ScheduleSelectBaseAdapter(getActivity(), scheduleArrayList);
                              listView.setAdapter(scheduleBaseAdapter);
                              android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity());
                              android.app.AlertDialog alertDialog = builder.create();
                              alertDialog.setView(inflate);
                              // 初始化弹窗数据
                              TextView zd_jl = inflate.findViewById(R.id.zd_jl);
                              zd_jl.setText(Constant.MAX_DESTINATION_DISTANCE);
                              TextView scheduleSld = inflate.findViewById(R.id.scheduleSld);
                              scheduleSld.setText(Constant.scheduleSld);

                              alertDialog.show();
                              Window window = alertDialog.getWindow();
                              WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                              lp.copyFrom(alertDialog.getWindow().getAttributes());

                              lp.width = Constant.dm.widthPixels * 4 / 5 + 150;
                              window.setAttributes(lp);
                              // 获取activity里面的按钮
                              Button closeSelectSchedule = inflate.findViewById(R.id.closeSelectSchedule);
                              closeSelectSchedule.setOnClickListener(view1 -> alertDialog.dismiss());
                              Button saveSelectSchedule = inflate.findViewById(R.id.saveSelectSchedule);
                              saveSelectSchedule.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View view) {
                                      // 读取终点距离
                                      TextView zd_jl = inflate.findViewById(R.id.zd_jl);
                                      Constant.MAX_DESTINATION_DISTANCE = zd_jl.getText().toString();

                                      // 读取顺路度
                                      TextView scheduleSld = inflate.findViewById(R.id.scheduleSld);
                                      Constant.scheduleSld = scheduleSld.getText().toString();

                                      if (Constant.xc_id.isEmpty()) {
                                          DialogUtil.showDialog(Constant.context, "至少选择一个行程");
                                      } else {
                                          // 匹配规则符合开始抢单 行程 小程序逻辑
                                          startCore.setText("停止抢单");
                                          Constant.isStart = true;
                                          Constant.executorService.execute(new MainThread());
                                          // 匹配规则符合开始抢单
                                          Constant.handler.post(() -> {
                                              DialogUtil.showDialog(Constant.context, "百里开始抢单");
                                          });
                                          // 获取自己接单的订单集合
                                          getOrderList();
                                          // 保存配置
                                          saveConfig();
                                          alertDialog.dismiss();

                                      }
                                  }
                              });
                          });
                       }
                   });
                } else {
                    // 大厅逻辑
                    startCore.setText("停止抢单");
                    Constant.isStart = true;
                    Constant.executorService.execute(new MainThread());
                    // 匹配规则符合开始抢单
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context, "百里开始抢单");
                    });
                    // 获取自己接单的订单集合
                    getOrderList();
                    // 保存配置
                    saveConfig();
                }
            }
        }
        // 虚拟定位按钮
        if (R.id.position == id) {
            FragmentActivity activity = getActivity();
            if (activity != null) {
                SelectLocationBDActivity.Companion.startActivityForResult(activity, 99);
            }
        }
        // 实时定位按钮
        if (R.id.ssdw_button == id) {
            Constant.executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String info = Constant.SS_DW_INFO;
                    if (info.isEmpty()) {
                        mainHandler.post(() -> DialogUtil.showDialog(Constant.context, "实时定位失败."));
                        return;
                    }
                    binding.startAddressText.setText(info);
                    Constant.cityInfo = HttpControllerUtil.getCityInfo(info);
                    if (Constant.cityInfo != null) {
                        mainHandler.post(() -> DialogUtil.showDialog(Constant.context, "定位成功."));
                    } else {
                        mainHandler.post(() -> DialogUtil.showDialog(Constant.context, "定位失败."));
                    }
                }
            });
        }
        // 计算刷新按钮
        if (R.id.js == id) {
            final TextView textView = binding.jsNumber;
            HomeViewModel notificationsViewModel =
                    new ViewModelProvider(this).get(HomeViewModel.class);

            int size = 0;
            int errorSize = 0;
            if (Constant.SON_TOKENS.isEmpty()) {
                DialogUtil.showDialog("刷单提示", "当前无小号为你设置安全刷新值为500").show();
                notificationsViewModel.getText("500").observe(getViewLifecycleOwner(), textView::setText);
                return;
            }
            for (TokenObject sonToken : Constant.SON_TOKENS) {
                if (sonToken.getStatus().equals("正常") && sonToken.getType().equals("1")) {
                    size = size + 1;
                } else if (!sonToken.getStatus().equals("正常") && sonToken.getType().equals("1")) {
                    errorSize = errorSize + 1;
                }
            }
            if (size == 0) {
                DialogUtil.showDialog("刷单提示", "当前无小号为你设置安全刷新值为500" + "异常小号有:" + errorSize + "个").show();
                notificationsViewModel.getText("500").observe(getViewLifecycleOwner(), textView::setText);
            } else {
                int number = 500 / size;
                DialogUtil.showDialog("刷单提示", "当前小号有:" + size + "个为你设置最佳为:" + number + "异常小号有:" + errorSize + "个").show();
                notificationsViewModel.getText(String.valueOf(number)).observe(getViewLifecycleOwner(), textView::setText);
            }

        }
        // 开始时间选择按钮
        if (R.id.select_start_time == id) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(Constant.context, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    month = month + 1;
                    // 弹出时分选择器
                    Calendar calendar = Calendar.getInstance();
                    int finalMonth = month;
                    TimePickerDialog timePickerDialog = new TimePickerDialog(Constant.context, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int i, int i1) {
                            String a = "";
                            String b = "";
                            a = String.valueOf(i);
                            if (i < 10) {
                                a = "0" + i;
                            }

                            b = String.valueOf(i1);
                            if (i1 < 10) {
                                b = "0" + i1;
                            }

                            String monthString = String.valueOf(finalMonth);
                            if (finalMonth < 10) {
                                monthString = "0" + (finalMonth);
                            }
                            String dayString = String.valueOf(day);
                            if (day < 10) {
                                dayString = "0" + day;
                            }
                            Constant.START_TIME_STRING = year + "-" + monthString + "-" + dayString + " " + a + ":" + b + ":00";
                            // 负值给组建
                            binding.startTime.setText(year + "-" + monthString + "-" + dayString + " " + a + ":" + b + ":00");
                        }
                    }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                    timePickerDialog.show();
                }
            }, 2024, 8, 25);
            datePickerDialog.show();
        }
        // 结束时间选择按钮
        if (R.id.select_end_time == id) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(Constant.context, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    month = month + 1;
                    // 弹出时分选择器
                    Calendar calendar = Calendar.getInstance();
                    int finalMonth = month;
                    TimePickerDialog timePickerDialog = new TimePickerDialog(Constant.context, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int i, int i1) {
                            String a = "";
                            String b = "";
                            a = String.valueOf(i);
                            if (String.valueOf(i).length() == 1) {
                                a = "0" + i;
                            }

                            b = String.valueOf(i1);
                            if (String.valueOf(i1).length() == 1) {
                                b = "0" + i1;
                            }
                            String monthString = String.valueOf(finalMonth);
                            if (String.valueOf(finalMonth).length() == 1) {
                                monthString = "0" + (finalMonth);
                            }
                            String dayString = String.valueOf(day);
                            if (String.valueOf(day).length() == 1) {
                                dayString = "0" + day;
                            }
                            Constant.END_TIME_STRING = year + "-" + monthString + "-" + dayString + " " + a + ":" + b + ":00";
                            // 负值给组建
                            binding.endTime.setText(year + "-" + monthString + "-" + dayString + " " + a + ":" + b + ":00");

                        }
                    }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                    timePickerDialog.show();
                }
            }, 2024, 8, 25);
            datePickerDialog.show();

        }

    }

    @Override
    @SuppressLint("UseSwitchCompatOrMaterialCode")
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 接单模型
        RadioButton CJ_ORDER_CHECK = view.findViewById(R.id.cj);
        if (CJ_ORDER_CHECK != null) CJ_ORDER_CHECK.setChecked(Constant.CJ_ORDER);
        RadioButton XC_ORDER_CHECK = view.findViewById(R.id.xc);
        if (XC_ORDER_CHECK != null) XC_ORDER_CHECK.setChecked(Constant.XC_ORDER);
        RadioButton SN_ORDER_CHECK = view.findViewById(R.id.sn);
        if (SN_ORDER_CHECK != null) SN_ORDER_CHECK.setChecked(Constant.SN_ORDER);

        // 接口模式
        RadioButton APP_IS_CHECK = view.findViewById(R.id.appController);
        if (APP_IS_CHECK != null) APP_IS_CHECK.setChecked(Constant.APP_IS_CHECK);
        RadioButton XCX_IS_CHECK = view.findViewById(R.id.xcxController);
        if (XCX_IS_CHECK != null) XCX_IS_CHECK.setChecked(Constant.XCX_IS_CHECK);

        // 虚拟定位地址
        TextView START_ADDRESS = view.findViewById(R.id.startAddressText);
        if (START_ADDRESS != null) START_ADDRESS.setText(Constant.START_ADDRESS);


        // 接单类型
        CheckBox SSDW_IS_CHECK = view.findViewById(R.id.ssdw);
        if (SSDW_IS_CHECK != null) SSDW_IS_CHECK.setChecked(Constant.SS_DW_CHECKED);


        // 最小金额和最大金额
        TextView MIN_MONEY = view.findViewById(R.id.minMoney);
        if (MIN_MONEY != null) MIN_MONEY.setText(String.valueOf(Constant.MIN_MONEY));
        TextView MAX_MONEY = view.findViewById(R.id.maxMoney);
        if (MAX_MONEY != null) MAX_MONEY.setText(String.valueOf(Constant.MAX_MONEY));

        // 开始时间和结束时间
        if (Constant.START_TIME_STRING == null || Constant.START_TIME_STRING.isEmpty())
            Constant.START_TIME_STRING = simpleDateFormat.format(new Date());
        if (Constant.END_TIME_STRING == null || Constant.END_TIME_STRING.isEmpty()) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Constant.END_TIME_STRING = simpleDateFormat.format(calendar.getTime());
        }
        TextView START_TIME = view.findViewById(R.id.startTime);
        if (START_TIME != null) START_TIME.setText(Constant.START_TIME_STRING);
        TextView END_TIME = view.findViewById(R.id.endTime);
        if (END_TIME != null) END_TIME.setText(Constant.END_TIME_STRING);

        // 接单模型
        Switch ZJ_DX = view.findViewById(R.id.zjdxd);
        if (ZJ_DX != null) ZJ_DX.setChecked(Constant.ZJ_DX);
        Switch YD_GS = view.findViewById(R.id.ydgsf);
        if (YD_GS != null) YD_GS.setChecked(Constant.YD_GS);
        Switch LX_QD = view.findViewById(R.id.lxqdd);
        if (LX_QD != null) LX_QD.setChecked(Constant.LX_QD);

        // 起点和终点
        TextView MAX_STARTING_POINT_DISTANCE = view.findViewById(R.id.max_starting_point_distance);
        if (MAX_STARTING_POINT_DISTANCE != null)
            MAX_STARTING_POINT_DISTANCE.setText(String.valueOf(Constant.MAX_STARTING_POINT_DISTANCE));

        // 订单排序规则
        RadioButton ZN_PX = view.findViewById(R.id.znpx);
        if (ZN_PX != null) ZN_PX.setChecked(Constant.ZN_PX_CHECK);

        RadioButton ZZ_CHECK = view.findViewById(R.id.zz);
        if (ZZ_CHECK != null) ZZ_CHECK.setChecked(Constant.ZZ_CHECK);

        RadioButton ZJ_CHECK = view.findViewById(R.id.zj);
        if (ZJ_CHECK != null) ZJ_CHECK.setChecked(Constant.ZJ_CHECK);

        RadioButton ZG_CHECK = view.findViewById(R.id.zg);
        if (ZG_CHECK != null) ZG_CHECK.setChecked(Constant.ZG_CHECK);


        // 接单类型
        CheckBox DX_IS_CHECK = view.findViewById(R.id.dxPersonCheckBox);
        if (DX_IS_CHECK != null) DX_IS_CHECK.setChecked(Constant.DX_PERSON_CHECKBOX);
        TextView DX_MIN_NUMBER = view.findViewById(R.id.minDxNumber);
        if (DX_MIN_NUMBER != null) DX_MIN_NUMBER.setText(String.valueOf(Constant.DX_MIN_NUMBER));
        TextView DX_MAX_NUMBER = view.findViewById(R.id.maxDxNumber);
        if (DX_MAX_NUMBER != null) DX_MAX_NUMBER.setText(String.valueOf(Constant.DX_MAX_NUMBER));

        CheckBox PC_IS_CHECK = view.findViewById(R.id.pcPersonCheckBox);
        if (PC_IS_CHECK != null) PC_IS_CHECK.setChecked(Constant.PC_PERSON_CHECKBOX);
        TextView PC_MIN_NUMBER = view.findViewById(R.id.minPcNumber);
        if (PC_MIN_NUMBER != null) PC_MIN_NUMBER.setText(String.valueOf(Constant.PC_MIN_NUMBER));
        TextView PC_MAX_NUMBER = view.findViewById(R.id.maxPcNumber);
        if (PC_MAX_NUMBER != null) PC_MAX_NUMBER.setText(String.valueOf(Constant.PC_MAX_NUMBER));


        CheckBox SS_IS_CHECK = view.findViewById(R.id.ssPersonCheckBox);
        if (SS_IS_CHECK != null) SS_IS_CHECK.setChecked(Constant.SS_PERSON_CHECKBOX);

        TextView SS_MIN_NUMBER = view.findViewById(R.id.minSsNumber);
        if (SS_MIN_NUMBER != null) SS_MIN_NUMBER.setText(String.valueOf(Constant.SS_MIN_NUMBER));
        TextView SS_MAX_NUMBER = view.findViewById(R.id.maxSsNumber);
        if (SS_MAX_NUMBER != null) SS_MAX_NUMBER.setText(String.valueOf(Constant.SS_MAX_NUMBER));

        // 单价设置
        CheckBox DJ_CHECK = view.findViewById(R.id.djCheckBox);
        if (DJ_CHECK != null) DJ_CHECK.setChecked(Constant.DJ_CHECK);
        TextView MIN_DJ_MONEY = view.findViewById(R.id.min_dj_money);
        if (MIN_DJ_MONEY != null) MIN_DJ_MONEY.setText(String.valueOf(Constant.DJ_MIN_MONEY));

        // 刷新时长
        TextView SX_NUMBER = view.findViewById(R.id.jsNumber);
        if (SX_NUMBER != null) SX_NUMBER.setText(Constant.SX_NUMBER);

        // 指定城市和排除城市
        CheckBox ZD_CS_CHECK = view.findViewById(R.id.zd_cs_check);
        if (ZD_CS_CHECK != null) ZD_CS_CHECK.setChecked(Constant.ZD_CS_CHECK);
        TextView ZD_CS_TEXT = view.findViewById(R.id.zd_cs_text);
        if (ZD_CS_TEXT != null)
            ZD_CS_TEXT.setText(String.valueOf(Constant.ZD_CS_TEXT.replaceAll(",", "#")));

        CheckBox PC_CS_CHECK = view.findViewById(R.id.pc_cs_check);
        if (PC_CS_CHECK != null) PC_CS_CHECK.setChecked(Constant.PC_CS_CHECK);
        TextView PC_CS_TEXT = view.findViewById(R.id.pc_cs_text);
        if (PC_CS_TEXT != null)
            PC_CS_TEXT.setText(String.valueOf(Constant.PC_CS_TEXT.replaceAll(",", "#")));

        Switch ZD_FB_XC = view.findViewById(R.id.zd_fb_xc);
        if (ZD_FB_XC != null) ZD_FB_XC.setChecked(Constant.ZD_FB_XC);
    }

    /**
     * @param text 地址
     * @Deprecated 修改实时定位地址
     */
    public static void updateStartAddressText(String text) {
        address.setText(text);
    }

    /**
     * @param text 需要修改的文职
     * @Deprecated 修改启动按钮标题
     */
    public static void updateStartCodeText(String text) {
        Constant.handler.post(() -> {
            Constant.errorMsg.setText(text);
            Constant.errorMsg.setTextColor(Color.RED);
        });
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Constant.handler.post(() -> {
            Constant.errorMsg.setText("展无消息");
            Constant.errorMsg.setTextColor(Color.BLACK);
        });
    }

    /**
     * @Deprecated 保存配置到数据库
     */
    public static void saveConfig() {
        SysUserConfig sysUserConfig = new SysUserConfig();
        sysUserConfig.setAccount(Constant.USER_ACCOUNT);

        if (Constant.CJ_ORDER)
            sysUserConfig.setJdMs("0");
        if (Constant.SN_ORDER)
            sysUserConfig.setJdMs("1");
        if (Constant.XC_ORDER)
            sysUserConfig.setJdMs("2");

        if (Constant.APP_IS_CHECK)
            sysUserConfig.setRsMs("0");
        if (Constant.XCX_IS_CHECK)
            sysUserConfig.setRsMs("1");

        if (Constant.SS_DW_CHECKED) {
            sysUserConfig.setIsDw("1");
        } else {
            sysUserConfig.setIsDw("0");
        }

        sysUserConfig.setMinMoney(Constant.MIN_MONEY.toString());
        sysUserConfig.setMaxMoney(Constant.MAX_MONEY.toString());

        if (Constant.ZJ_DX)
            sysUserConfig.setGlqSelect("0");
        if (Constant.YD_GS)
            sysUserConfig.setGlqSelect("1");
        if (Constant.LX_QD)
            sysUserConfig.setGlqSelect("2");

        sysUserConfig.setMin_start(Constant.MAX_STARTING_POINT_DISTANCE);

        if (Constant.ZN_PX_CHECK)
            sysUserConfig.setPx_gz("0");
        if (Constant.ZJ_CHECK)
            sysUserConfig.setPx_gz("1");
        if (Constant.ZZ_CHECK)
            sysUserConfig.setPx_gz("2");
        if (Constant.ZG_CHECK)
            sysUserConfig.setPx_gz("3");

        if (Constant.PC_PERSON_CHECKBOX) {
            sysUserConfig.setPc_check("1");
        } else {
            sysUserConfig.setPc_check("0");
        }
        sysUserConfig.setMin_pc_number(Constant.PC_MIN_NUMBER.toString());
        sysUserConfig.setMax_pc_number(Constant.PC_MAX_NUMBER.toString());


        if (Constant.DJ_CHECK) {
            sysUserConfig.setDj_check("1");
        } else {
            sysUserConfig.setDj_check("0");
        }
        sysUserConfig.setMin_dj_money(String.valueOf(Constant.DJ_MIN_MONEY));

        if (Constant.DX_PERSON_CHECKBOX) {
            sysUserConfig.setDx_check("1");
        } else {
            sysUserConfig.setDx_check("0");
        }
        sysUserConfig.setMin_dx_number(Constant.DX_MIN_NUMBER.toString());
        sysUserConfig.setMax_dx_number(Constant.DX_MAX_NUMBER.toString());


        if (Constant.SS_PERSON_CHECKBOX) {
            sysUserConfig.setSs_check("1");
        } else {
            sysUserConfig.setSs_check("0");
        }
        sysUserConfig.setMin_ss_number(Constant.SS_MIN_NUMBER.toString());
        sysUserConfig.setMax_ss_number(Constant.SS_MAX_NUMBER.toString());
        Constant.executorService.execute(new Runnable() {
            @Override
            public void run() {
                // 先删除再增
                MyHttpUtil.saveUserConfig(sysUserConfig);
            }
        });
    }

    /**
     * @Deprecated 初始化自己目前已经有的单子
     */
    public static void getOrderList() {
        // 订单列表初始化
        Constant.executorService.execute(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = HttpControllerUtil.getSuccessOrder();
                if (jsonObject.getString("code").equals("103")) {
                    Constant.handler.post(() -> {
                        DialogUtil.showDialog(Constant.context, "主号登陆已过期!");
                        // 赋值常量为关闭
                        Constant.isStart = false;
                        LogManager.get().stopLog();
                        FloatWindowManager.get().stopAnimator();
                        Constant.executorService.execute(new Runnable() {
                            @Override
                            public void run() {
                                // 修改主号状态
                                TokenObject tokenObject = new TokenObject(Constant.MAIN_TOKEN,
                                        Constant.MAIN_TICKET, "1", "1", 0, "1", Constant.USER_ACCOUNT);
                                MyHttpUtil.updateUserTokenStatus(tokenObject);
                            }
                        });
                    });
                    return;
                }
                if (jsonObject != null && jsonObject.getString("code").equals("0")) {
                    // 获取行程数组
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (Object datum : data) {
                        JSONObject dataList = JSONObject.parseObject(datum.toString());
                        JSONArray paxOrderList = dataList.getJSONArray("paxOrderList");
                        for (Object o : paxOrderList) {
                            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                            String string = jsonObject1.getString("passengerJourneyGuid");
                            Constant.orderList.append(string).append(",");
                        }
                    }
                }
                Constant.CHECK_OK = true;
            }
        });
    }

}